package test.day03;

import test.entity.ListNode;

public class HasCycleLink {
	/**
	 * 快慢指针判定成环
	 * @param head
	 * @return
	 */
	public boolean hasCycle(ListNode head) {
		if (head == null || head.getNext() == null) {
			return false;
		}

		ListNode slow = head;
		ListNode fast = head;
		while (fast != null && fast.getNext() != null) {
			slow = slow.getNext();
			fast = fast.getNext().getNext();
			if (slow == fast) {
				return true;
			}
		}
		return false;
	}
}
